<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * @todo 线上家博会
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2016-3-9
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class OnlineExpoController extends AdminController{
	
    /* ------------------------------- 品类-方法 --------------------------- */
    
	/**
	 * @todo 品类管理列表
	 * @author sap
	 * @since 2016-3-9
	 */
	public function categoryList(){
		$search_text = strip_tags(I('search_text'));//搜索内容
		$map = array();//初始化查询条件
		//查询条件
		if($search_text != '' && $search_text != null){
			$map['category_name'] = array('like', '%'.(string)$search_text.'%');
		}
		//列表
		$map['status']=array('gt',-1);
		$list = $this->lists('OnlineCategory',$map,'category_id desc','category_id,category_name,sort');
		$this->assign('_list',$list);
		$this->assign('meta_title','品类管理');
		$this->display('category_list');
	}
	
	/**
	 * @todo 添加品类
	 * @author sap
	 * @since 2016-3-9
	 */
	public function categoryAdd(){
		if(IS_POST){
			$this->categoryUpdate();
		}else{
			$this->assign('list',null);
			$this->assign('meta_title','新增品类');
			$this->assign('meta_title_c','品类管理');
			$this->display('category_add');
		}
	}
	
	/**
	 * @todo 编辑品类
	 * @author sap
	 */
	public function categoryEdit(){
	    $category_id = (int)I('category_id');
	    if($category_id == 0){
	        $this->error('请正确操作');
	    }
		if(IS_POST){
			$this->categoryUpdate();
		}else{
			$info = D('OnlineCategory')->onlineCategoryShow($category_id,'category_id,category_name,sort');
			$this->assign('list',$info);
			$this->assign('meta_title','更新品类');
			$this->assign('meta_title_c','品类管理');
			$this->display('category_add');
		}
	}
	
	/**
	 * @todo 添加/编辑品类(仅供内部调用)
	 * @author sap
	 */
	private function categoryUpdate(){
		$obj = D('OnlineCategory');
		$id = $obj->update();
		if(empty($id)){
			$this->error($obj->getError());
		}
		$this->success('操作成功',U('OnlineExpo/categoryList'));
	}
	
	/**
	 * @todo 品类状态修改
	 * @param string $method 标识
	 * @author sap
	 */
	public function categoryChangeStatus($method=null){
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if(empty($id)) {
			$this->error('请选择要操作的数据!');
		}
		$map['category_id'] = array('in',$id);
		switch ( strtolower($method) ){
			case 'deletecategory':
			    //判断展馆下是否有品类
			    if(D('Admin/OnlineExpo')->onlineZoneCategoryList('',$id)){
			        $this->error('该品类在展馆下,不可删除');
			    }
			    //判断品类下是否有店铺
			    if(D('Admin/OnlineShop')->getOnlineShopList(array('online_category_id'=>array('in',$id),'status'=>array('gt',-1)),'online_shop_id')){
			        $this->error('品类下有店铺,不可删除');
			    }
			    //删除操作
				$this->delete('OnlineCategory', $map );
				//记录行为
				action_log('online_category_del', 'online_category', $id, UID);
				//删除缓存
				S('sys_online_category_', null);
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	/* ------------------------------- 展馆-方法 --------------------------- */
	
	/**
	 * @todo 展馆管理列表
	 * @author sap
	 * @since 2016-3-9
	 */
	public function zoneList(){
	    $map = array();
	    $map['status']=array('gt',-1);
	    $list = $this->lists('OnlineZone',$map,'activity_id desc,zone_id desc','zone_id,activity_id,zone_name,zone_title,sort');
	    $this->assign('_list',$list);
	    $this->assign('meta_title','展馆管理');
	    $this->display('zone_list');
	}
	
	/**
	 * @todo 添加展馆
	 * @author sap
	 */
	public function zoneAdd(){
	    if(IS_POST){
	        $this->zoneUpdate();
	    }else{
	        $this->getActivityList();
	        $this->assign('list',null);
	        $this->assign('meta_title','新增展馆');
	        $this->assign('meta_title_c','展馆管理');
	        $this->display('zone_add');
	    }
	}
	
	/**
	 * @todo 编辑展馆
	 * @author sap
	 */
	public function zoneEdit(){
	    $zone_id = (int)I('zone_id');
	    if($zone_id == 0){
	        $this->error('请正确操作');
	    }
	    if(IS_POST){
	        $this->zoneUpdate();
	    }else{
	        $info = D('OnlineZone')->onlineZoneShow($zone_id,'zone_id,activity_id,zone_name,zone_title,sort');
	        $this->assign('list',$info);
	        $this->getActivityList();
	        $this->assign('meta_title','更新展馆');
	        $this->assign('meta_title_c','展馆管理');
	        $this->display('zone_add');
	    }
	}
	
	/**
	 * @todo 添加/编辑品类(仅供内部调用)
	 * @author sap
	 */
	private function zoneUpdate(){
	    $obj = D('OnlineZone');
	    $id = $obj->update();
	    if(empty($id)){
	        $this->error($obj->getError());
	    }
	    $this->success('操作成功',U('OnlineExpo/zoneList'));
	}
	
	/**
	 * @todo 展馆状态修改
	 * @param string $method 标识
	 * @author sap
	 */
	public function zoneChangeStatus($method=null){
	    $id = array_unique((array)I('id',0));
	    $id = is_array($id) ? implode(',',$id) : $id;
	    if(empty($id)) {
	        $this->error('请选择要操作的数据!');
	    }
	    $map['zone_id'] = array('in',$id);
	    switch ( strtolower($method) ){
	        case 'deletezone':
	            //判断展馆下是否有品类
	            if(D('Admin/OnlineExpo')->onlineZoneCategoryList($id)){
	                $this->error('展馆下有品类,不可删除');
	            }
	            //删除操作
	            $this->delete('OnlineZone', $map );
	            //记录行为
	            action_log('online_zone_del', 'online_zone', $id, UID);
	            //删除缓存
	            S('sys_online_zone_', null);
	            break;
	        default:
	            $this->error('参数非法');
	    }
	}
	
	/* ------------------------------- 展馆品类-方法 --------------------------- */
	
	/**
	 * @todo 展馆品类列表
	 * @author sap
	 * @since 2016-3-10
	 */
	public function zoneCategoryList(){
	    $zone_id = (int)I('zone_id');
	    //查询条件
	    $map = array();
	    if($zone_id != 0){
	        $map['zone_id'] = $zone_id;
	    }
	    //列表
	    $list = $this->lists('OnlineZoneCategory',$map,'online_category_id desc');
	    //显示
	    $this->assign('_list',$list);
	    $this->assign('meta_title_c','展馆管理');
	    $this->assign('meta_title','展馆品类管理');
	    $this->display('zone_category_list');
	}
	
	/**
	 * @todo 添加展馆品类
	 * @author sap
	 * @since 2016-3-10
	 */
	public function zoneCategoryAdd(){
	    $zone_id = (int)I('zone_id');
	    if($zone_id == 0){
	        $this->error('选择展馆');
	    }
	    if(IS_POST){
	        $obj = D('Admin/OnlineExpo');
	        if(!$obj->zoneCategoryAdd()){
	            $this->error($obj->getError());
	        }
	        $this->success('操作成功',U('OnlineExpo/zoneCategoryList',array('zone_id'=>$zone_id)));
	    }else{
	        $info = D('OnlineZone')->onlineZoneShow($zone_id,'zone_id,activity_id,zone_name');
	        $this->assign('list',$info);
	        $this->getOnlineCategoryList();//品类列表
	        $this->assign('meta_title_c','展馆品类管理');
	        $this->assign('meta_title','新增展馆品类');
	        $this->display('zone_category_add');
	    }
	}
	
	/**
	 * @todo 删除展馆与品类的关系
	 * @author sap
	 */
	public function zoneCategoryDel(){
	    $obj = D('Admin/OnlineExpo');
	    if(!$obj->zoneCategoryDel()){
	        $this->error($obj->getError());
	    }else{
	        $this->success('删除成功');
	    }
	}
	
	/* ------------------------------- 店铺-方法 --------------------------- */
	
	/**
	 * @todo 店铺管理列表
	 * @author sap
	 * @since 2016-3-10
	 */
	public function shopList(){
	    $search_text = strip_tags(I('search_text'));//搜索内容
	    $map = array();//初始化查询条件
	    //查询条件
	    if($search_text != '' && $search_text != null){
	        $map['online_shop_name'] = array('like', '%'.(string)$search_text.'%');
	    }
	    //列表
	    $map['status']=array('gt',-1);
	    $list = $this->lists('OnlineShop',$map,'online_shop_id desc','online_shop_id,activity_id,zone_id,online_category_id,online_shop_name,sort');
	    $this->assign('_list',$list);
	    $this->assign('meta_title','店铺管理');
	    $this->display('shop_list');
	}
	
    /**
     * @todo 添加店铺
     * @author sap
     * @since 2016-3-11
     */
	public function shopAdd(){
	    if(IS_POST){
	        $this->shopUpdate();
	    }else{
	        $this->getActivityList();
	        $this->assign('list',null);
	        $this->assign('meta_title','新增店铺');
	        $this->assign('meta_title_c','店铺管理');
	        $this->display('shop_add');
	    }
	}
	
	/**
	 * @todo 编辑店铺
	 * @author sap
	 */
	public function shopEdit(){
	    $online_shop_id = (int)I('online_shop_id');
	    if($online_shop_id == 0){
	        $this->error('请正确操作');
	    }
	    if(IS_POST){
	        $this->shopUpdate();
	    }else{
	        $info = D('OnlineShop')->onlineShopShow($online_shop_id);
	        $zone_list = $this->getActivityZone($info['activity_id'],0);
	        $category_list = $this->getActivityZoneCategory($info['zone_id'],0);
	        
	        $this->assign('list',$info);
	        $this->getActivityList();
	        $this->assign('zone_list',$zone_list);
	        $this->assign('category_list',$category_list);
	        
	        $this->assign('meta_title','更新店铺');
	        $this->assign('meta_title_c','店铺管理');
	        $this->display('shop_add');
	    }
	}
	
	/**
	 * @todo 添加/编辑店铺(仅供内部调用)
	 * @author sap
	 */
	private function shopUpdate(){
	    $obj = D('OnlineShop');
	    $id = $obj->update();
	    if(empty($id)){
	        $this->error($obj->getError());
	    }
	    $this->success('操作成功',U('OnlineExpo/shopList'));
	}
	
	/**
	 * @todo 店铺状态修改
	 * @param string $method 标识
	 * @author sap
	 */
	public function shopChangeStatus($method=null){
	    $id = array_unique((array)I('id',0));
	    $id = is_array($id) ? implode(',',$id) : $id;
	    if(empty($id)) {
	        $this->error('请选择要操作的数据!');
	    }
	    $map['online_shop_id'] = array('in',$id);
	    switch ( strtolower($method) ){
	        case 'deleteshop':
	            //判断店铺下是否有商品
	            if(D('Admin/OnlineShopGoods')->getOnlineShopGoodsList(array('online_shop_id'=>array('in',$id),'status'=>array('gt',-1)),'online_goods_id')){
	                $this->error('店铺下有商品,不可删除');
	            }
	            //删除操作
	            $this->delete('OnlineShop', $map);
	            //记录行为
	            action_log('online_shop_del', 'online_shop', $id, UID);
	            //删除缓存
	            S('sys_online_shop_', null);
	            break;
	        default:
	            $this->error('参数非法');
	    }
	}
	
	/* ------------------------------- 商品-方法 --------------------------- */
	
	/**
	 * @todo 商品管理列表
	 * @author sap
	 * @since 2016-3-12
	 */
	public function goodsList(){
	    $activity_id = (int)I('activity_id');
	    $online_shop_id = (int)I('online_shop_id');
	    $search_text = strip_tags(I('search_text'));//搜索内容
	    //查询条件
	    $map = array();//初始化查询条件
	    if($search_text != '' && $search_text != null){
	        $map['online_goods_name'] = array('like', '%'.(string)$search_text.'%');
	    }
	    if($activity_id != 0){
	        $map['activity_id'] = $activity_id;
	    }
	    if($online_shop_id != 0){
	        $map['online_shop_id'] = $online_shop_id;
	    }
	    //列表
	    if(empty($map)){
	        $list = null;
	    }else{
	        $map['status']=array('gt',-1);
	        $list = $this->lists('OnlineShopGoods',$map,'online_goods_id desc','online_goods_id,online_shop_id,online_goods_name,market_price,activity_price,sort');
	    }
	    $this->assign('_list',$list);
	    
	    $this->getActivityList();
	    $shop_list = $this->getActivityShop($activity_id,0);
	    $this->assign('shop_list',$shop_list);
	    
	    $this->assign('meta_title','商品管理');
	    $this->display('goods_list');
	}
	
	/**
	 * @todo 添加商品
	 * @author sap
	 * @since 2016-3-12
	 */
	public function goodsAdd(){
	    if(IS_POST){
	        $this->goodsUpdate();
	    }else{
	        $this->getActivityList();
	        $this->assign('list',null);
	        $this->assign('meta_title','新增商品');
	        $this->assign('meta_title_c','商品管理');
	        $this->display('goods_add');
	    }
	}
	
	/**
	 * @todo 编辑商品
	 * @author sap
	 */
	public function goodsEdit(){
	    $online_goods_id = (int)I('online_goods_id');
	    if($online_goods_id == 0){
	        $this->error('请正确操作');
	    }
	    if(IS_POST){
	        $this->goodsUpdate();
	    }else{
	        $info = D('OnlineShopGoods')->onlineShopGoodsShow($online_goods_id);
	        $shop_list = $this->getActivityShop($info['activity_id'],0);
	        $this->assign('list',$info);
	        $this->getActivityList();
	        $this->assign('shop_list',$shop_list);
	        $this->assign('meta_title','更新商品');
	        $this->assign('meta_title_c','商品管理');
	        $this->display('goods_add');
	    }
	}
	
	/**
	 * @todo 添加/编辑商品(仅供内部调用)
	 * @author sap
	 */
	private function goodsUpdate(){
	    $obj = D('OnlineShopGoods');
	    $id = $obj->update();
	    if(empty($id)){
	        $this->error($obj->getError());
	    }
	    $this->success('操作成功',U('OnlineExpo/goodsList'));
	}
	
	/**
	 * @todo 商品状态修改
	 * @param string $method 标识
	 * @author sap
	 */
	public function goodsChangeStatus($method=null){
	    $id = array_unique((array)I('id',0));
	    $id = is_array($id) ? implode(',',$id) : $id;
	    if(empty($id)) {
	        $this->error('请选择要操作的数据!');
	    }
	    $map['online_goods_id'] = array('in',$id);
	    switch ( strtolower($method) ){
	        case 'deletegoods':
	            //删除操作
	            $this->delete('OnlineShopGoods', $map );
	            //记录行为
	            action_log('online_shop_goods_del', 'online_shop_goods', $id, UID);
	            break;
	        default:
	            $this->error('参数非法');
	    }
	}
	
	/* ------------------------------- 功能页-方法 --------------------------- */
	
	/**
	 * @todo 功能页管理列表
	 * @author sap
	 * @since 2016-3-11
	 */
	public function onePageList(){
	    $search_text = strip_tags(I('search_text'));//搜索内容
	    $map = array();//初始化查询条件
	    //查询条件
	    if($search_text != '' && $search_text != null){
	        $map['page_name'] = array('like', '%'.(string)$search_text.'%');
	    }
	    //列表
	    $map['status']=array('gt',-1);
	    $list = $this->lists('OnlineOnePage',$map,'page_id desc','page_id,page_name,sort');
	    $this->assign('_list',$list);
	    $this->assign('meta_title','功能页管理');
	    $this->display('one_page_list');
	}
	
	/**
	 * @todo 添加功能页
	 * @author sap
	 * @since 2016-3-12
	 */
	public function onePageAdd(){
	    if(IS_POST){
	        $this->onePageUpdate();
	    }else{
	        $this->assign('list',null);
	        $this->assign('meta_title','新增功能页');
	        $this->assign('meta_title_c','功能页管理');
	        $this->display('one_page_add');
	    }
	}
	
	/**
	 * @todo 编辑功能页
	 * @author sap
	 */
	public function onePageEdit(){
	    $page_id = (int)I('page_id');
	    if($page_id == 0){
	        $this->error('请正确操作');
	    }
	    if(IS_POST){
	        $this->onePageUpdate();
	    }else{
	        $info = D('OnlineOnePage')->onlineOnePageShow($page_id,'page_id,page_name,page_content,page_place,page_des,sort');
	        $this->assign('list',$info);
	        $this->assign('meta_title','更新功能页');
	        $this->assign('meta_title_c','功能页管理');
	        $this->display('one_page_add');
	    }
	}
	
	/**
	 * @todo 添加/编辑功能页(仅供内部调用)
	 * @author sap
	 */
	private function onePageUpdate(){
	    $obj = D('OnlineOnePage');
	    $id = $obj->update();
	    if(empty($id)){
	        $this->error($obj->getError());
	    }
	    $this->success('操作成功',U('OnlineExpo/onePageList'));
	}
	
	/**
	 * @todo 功能状态修改
	 * @param string $method 标识
	 * @author sap
	 */
	public function onePageChangeStatus($method=null){
	    $id = array_unique((array)I('id',0));
	    $id = is_array($id) ? implode(',',$id) : $id;
	    if(empty($id)) {
	        $this->error('请选择要操作的数据!');
	    }
	    $map['page_id'] = array('in',$id);
	    switch ( strtolower($method) ){
	        case 'deletepage':
	            //删除操作
	            $this->delete('OnlineOnePage', $map );
	            //记录行为
	            action_log('online_one_page_del', 'online_one_page', $id, UID);
	            break;
	        default:
	            $this->error('参数非法');
	    }
	}
	
	/* ------------------------------- 公共-方法 --------------------------- */
	
	/**
	 * @todo 活动列表
	 * @author sap
	 */
	private function getActivityList(){
	    $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
	    $this->assign('activity_list',$activity_list);
	}
	
	/**
	 * @todo 品类管理
	 * @author sap
	 */
	private function getOnlineCategoryList(){
	    $category_list = D('OnlineCategory')->getOnlineCategoryList(array('status'=>array('gt',-1)),'category_id,category_name');
	    $this->assign('category_list',$category_list);
	}
	
	/**
	 * @todo 根据活动调取展馆（公共方法）不受权限控制
	 * @author sap
	 */
	public function getActivityZone($activity_id='',$data_type=''){
	    //获取
	    if($activity_id == ''){
	        $activity_id = (int)I('activity_id');
	    }
	    if($data_type == ''){
	        $data_type = (int)I('data_type');
	    }
	    //获取店铺数据
	    if($activity_id != 0){
	        $zone_list = D('OnlineZone')->getOnlineZoneList(array('activity_id'=>$activity_id,'status'=>1));
	    }else{
	        $zone_list = null;
	    }
	    //返回值
	    $data= array();
	    switch ($data_type){
	        case 0:
	            return $zone_list;
	            break;
	        case 1:
	            if(empty($zone_list)){
	                $data['status'] = 0;
	                $data['info'] = '没有数据';
	            }else{
	                $data['status'] = 1;
	                $data['data'] = $zone_list;
	            }
	            $this->ajaxReturn($data);
	            break;
	        default:
	            $this->error('参数有误');
	            break;
	    }
	}
	
	/**
	 * @todo 根据展馆调取品类（公共方法）不受权限控制
	 * @author sap
	 */
	public function getActivityZoneCategory($zone_id='',$data_type=''){
	    //获取
	    if($zone_id == ''){
	        $zone_id = (int)I('zone_id');
	    }
	    if($data_type == ''){
	        $data_type = (int)I('data_type');
	    }
	    //获取店铺数据
	    if($zone_id != 0){
	        $online_zone_category_list = D('Admin/OnlineExpo')->onlineZoneCategoryList($zone_id);
	        foreach ($online_zone_category_list as $key=>$val){
	            $online_zone_category_list[$key]['category_name'] = getOnlineCategoryName($val['online_category_id']);
	        }
	    }else{
	        $online_zone_category_list = null;
	    }
	    //返回值
	    $data= array();
	    switch ($data_type){
	        case 0:
	            return $online_zone_category_list;
	            break;
	        case 1:
	            if(empty($online_zone_category_list)){
	                $data['status'] = 0;
	                $data['info'] = '没有数据';
	            }else{
	                $data['status'] = 1;
	                $data['data'] = $online_zone_category_list;
	            }
	            $this->ajaxReturn($data);
	            break;
	        default:
	            $this->error('参数有误');
	            break;
	    }
	}

	/**
	 * @todo 根据活动调取店铺（公共方法）不受权限控制
	 * @author sap
	 */
	public function getActivityShop($activity_id='',$data_type=''){
	    //获取
	    if($activity_id == ''){
	        $activity_id = (int)I('activity_id');
	    }
	    if($data_type == ''){
	        $data_type = (int)I('data_type');
	    }
	    //获取店铺数据
	    if($activity_id != 0){
	        $shop_list = D('OnlineShop')->getOnlineShopList(array('activity_id'=>$activity_id,'status'=>array('gt',-1)));
	    }else{
	        $shop_list = null;
	    }
	    //返回值
	    $data= array();
	    switch ($data_type){
	        case 0:
	            return $shop_list;
	            break;
	        case 1:
	            if(empty($shop_list)){
	                $data['status'] = 0;
	                $data['info'] = '没有数据';
	            }else{
	                $data['status'] = 1;
	                $data['data'] = $shop_list;
	            }
	            $this->ajaxReturn($data);
	            break;
	        default:
	            $this->error('参数有误');
	            break;
	    }
	}
	
}